using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using JetBrains.Annotations;
using Voodoo.Sauce.Internal.Extension;

namespace Voodoo.Analytics
{
	internal class VanEvent
	{
		private struct TrackingIdParameters
		{
			public string eventId;

			public string name;

			public string type;

			public string sessionId;

			public string createdAt;

			public string advertisingId;

			public string bundleId;

			public string platform;

			public string developerDeviceId;

			public string secretKey;
		}

		private const string EVENT_TYPE_IMPRESSION = "impression";

		private const string EVENT_TYPE_APP = "app";

		private const string EVENT_TYPE_CUSTOM = "custom";

		private Dictionary<string, object> _values;

		private string _name;

		private string _jsonData;

		private string _customVariablesData;

		private string _contextVariablesData;

		private static readonly string[] ImpressionEvents = new string[36]
		{
			VanEventName.fs_shown.ToString(),
			VanEventName.fs_click.ToString(),
			VanEventName.fs_watched.ToString(),
			VanEventName.fs_trigger.ToString(),
			VanEventName.fs_failed.ToString(),
			VanEventName.rv_shown.ToString(),
			VanEventName.rv_click.ToString(),
			VanEventName.rv_watched.ToString(),
			VanEventName.rv_trigger.ToString(),
			VanEventName.rv_failed.ToString(),
			VanEventName.banner_shown.ToString(),
			VanEventName.banner_click.ToString(),
			VanEventName.thumbnail_click.ToString(),
			VanEventName.thumbnail_shown.ToString(),
			VanEventName.native_shown.ToString(),
			VanEventName.native_click.ToString(),
			VanEventName.native_trigger.ToString(),
			VanEventName.native_closed.ToString(),
			VanEventName.native_failed.ToString(),
			VanEventName.ad_revenue.ToString(),
			VanEventName.cp_impression.ToString(),
			VanEventName.cp_click.ToString(),
			VanEventName.cp_response_status.ToString(),
			VanEventName.attribution_changed.ToString(),
			VanEventName.audio_ad_shown.ToString(),
			VanEventName.audio_ad_clicked.ToString(),
			VanEventName.audio_ad_trigger.ToString(),
			VanEventName.audio_ad_closed.ToString(),
			VanEventName.audio_ad_watched.ToString(),
			VanEventName.audio_ad_failed.ToString(),
			VanEventName.attribution_changed.ToString(),
			VanEventName.ao_trigger.ToString(),
			VanEventName.ao_shown.ToString(),
			VanEventName.ao_clicked.ToString(),
			VanEventName.ao_watched.ToString(),
			VanEventName.ao_failed.ToString()
		};

		internal string GetName()
		{
			return _name;
		}

		private VanEvent()
		{
		}

		internal static void Create(string name, VanEventBasicParameters parameters, string dataJson, string customVariablesJson, string contextVariablesJson, string type, Action<VanEvent> complete, [CanBeNull] string eventId, VanSessionInfo sessionInfo)
		{
			eventId = eventId ?? Guid.NewGuid().ToString();
			type = type ?? GetType(name);
			string text = DateTime.UtcNow.ToIsoFormat();
			TrackingIdParameters parameters2 = default(TrackingIdParameters);
			parameters2.eventId = eventId;
			parameters2.name = name;
			parameters2.type = type;
			parameters2.sessionId = sessionInfo.id;
			parameters2.createdAt = text;
			parameters2.advertisingId = parameters.GetAdvertisingId();
			parameters2.bundleId = parameters.GetAppBundleId();
			parameters2.platform = parameters.GetPlatform();
			parameters2.developerDeviceId = parameters.GetDeveloperDeviceId();
			parameters2.secretKey = "p2s5v8y/B?E(H+MbPeShVmYq3t6w9z$C&F)J@NcRfTjWnZr4u7x!A%D*G-KaPdSgVkXp2s5v8y/B?E(H+MbQeThWmZq3t6w9z$C&F)J@NcRfUjXn2r5u7x!A%D*G-KaP";
			string trackingId = GetTrackingId(parameters2);
			Dictionary<string, object> dictionary = new Dictionary<string, object>
			{
				{ "id", eventId },
				{ "name", name },
				{ "type", type },
				{ "created_at", text },
				{ "session_id", sessionInfo.id },
				{ "session_length", sessionInfo.length },
				{ "session_count", sessionInfo.count },
				{
					"vs_version",
					parameters.GetVSVersion()
				},
				{
					"user_id",
					parameters.GetUserId()
				},
				{
					"bundle_id",
					parameters.GetAppBundleId()
				},
				{
					"app_version",
					parameters.GetAppVersion()
				},
				{
					"locale",
					parameters.GetLocale()
				},
				{
					"connectivity",
					parameters.GetConnectivity()
				},
				{
					"app_open_count",
					parameters.GetAppLaunchCount()
				},
				{
					"user_game_count",
					parameters.GetGameCount()
				},
				{
					"screen_resolution",
					parameters.GetScreenResolution()
				},
				{
					"os_version",
					parameters.GetOSVersion()
				},
				{
					"manufacturer",
					parameters.GetManufacturer()
				},
				{
					"model",
					parameters.GetModel()
				},
				{
					"developer_device_id",
					parameters.GetDeveloperDeviceId()
				},
				{
					"ads_consent_given",
					parameters.GetAdsConsentGiven()
				},
				{
					"analytics_consent_given",
					parameters.GetAnalyticsConsentGiven()
				},
				{
					"platform",
					parameters.GetPlatform()
				},
				{
					"advertising_id",
					parameters.GetAdvertisingId()
				},
				{
					"limit_ad_tracking",
					parameters.HasLimitAdTrackingEnabled()
				},
				{ "tracking_id", trackingId }
			};
			dictionary.AddIfNotNull("current_level", parameters.GetCurrentLevel());
			dictionary.AddIfNotNull("idfa_authorization_status", parameters.GetIdfaAuthorizationStatus());
			dictionary.AddIfNotNull("mediation", parameters.GetMediation());
			dictionary.AddIfNotNull("segment_uuid", parameters.GetSegmentationUuid());
			dictionary.AddIfNotNull("ab_test_uuid", parameters.GetAbTestUuid());
			dictionary.AddIfNotNull("cohort_uuid", parameters.GetAbTestCohortUuid());
			dictionary.AddIfNotNull("install_store", parameters.GetInstallStore());
			dictionary.AddIfNotNull("first_app_launch_date", parameters.GetFirstAppLaunchDate());
			dictionary.AddIfNotNull("version_uuid", parameters.GetAbTestVersionUuid());
			dictionary.AddIfNotNull("attribution_platform", parameters.GetAttributionDataName());
			complete(new VanEvent
			{
				_values = dictionary,
				_name = name,
				_jsonData = dataJson,
				_customVariablesData = customVariablesJson,
				_contextVariablesData = contextVariablesJson
			});
		}

		private static string GetTrackingId(TrackingIdParameters parameters)
		{
			Dictionary<string, object> dic = new Dictionary<string, object>
			{
				{ "id", parameters.eventId },
				{ "name", parameters.name },
				{ "type", parameters.type },
				{ "session_id", parameters.sessionId },
				{ "created_at", parameters.createdAt },
				{ "advertising_id", parameters.advertisingId },
				{ "bundle_id", parameters.bundleId },
				{ "platform", parameters.platform },
				{ "developer_device_id", parameters.developerDeviceId }
			};
			byte[] bytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(dic.ToJson())));
			using HMACSHA256 hMACSHA = new HMACSHA256(Encoding.UTF8.GetBytes(parameters.secretKey));
			return Convert.ToBase64String(hMACSHA.ComputeHash(bytes));
		}

		private static string GetType(string name)
		{
			if (!Enum.IsDefined(typeof(VanEventName), name))
			{
				return "custom";
			}
			if (ImpressionEvents.Contains(name))
			{
				return "impression";
			}
			return "app";
		}

		internal string ToJson()
		{
			return AnalyticsUtil.ConvertDictionaryToJson(_values, _jsonData, _customVariablesData, _contextVariablesData);
		}

		public override string ToString()
		{
			return _values.ToString();
		}
	}
}
